package com.itopia.team04;
import java.util.Timer;
import java.util.TimerTask;

import com.itopia.team04.models.SlideMenuHelper;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnClickListener {
	
	private SlideMenuHelper smh;
	private Button sb;	
	private LinearLayout  horizontalOuterLayout;
	private HorizontalScrollView horizontalScrollview;	
	private int scrollMax;
	private int scrollPos =	0;
	private TimerTask clickSchedule;
	private TimerTask scrollerSchedule;
	private TimerTask faceAnimationSchedule;
	private Button clickedButton	=	null;
	private Timer scrollTimer		=	null;
	private Timer clickTimer		=	null;
	private Timer faceTimer         =   null;
	private Boolean isFaceDown      =   true;
	private String[] nameArray = {"Apple", "Banana", "Grapes", "Orange", "Strawberry","Apple", "Banana"};
	private String[] imageNameArray = {"apple", "banana", "grapes", "orange", "strawberry","apple", "banana"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
		initHoriview();
	}
	
	private void initHoriview()
	{
		horizontalScrollview  = (HorizontalScrollView) findViewById(R.id.horiztonal_scrollview_id);
        horizontalOuterLayout =	(LinearLayout)findViewById(R.id.horiztonal_outer_layout_id);      
        
        horizontalScrollview.setHorizontalScrollBarEnabled(false);
        horizontalScrollview.setPadding(0, 75, 0, 5);
        addImagesToView();
        
		ViewTreeObserver vto 		=	horizontalOuterLayout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
            	horizontalOuterLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            	getScrollMaxAmount();
            	//startAutoScrolling();            	          	
            }
        });
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			smh.close();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void init()
	{
		sb = (Button) findViewById(R.id.slide_button);
		sb.setOnClickListener(this);
	}
	
	public SlideMenuHelper getMenuHelper(){
		return smh;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId())
		{
		case R.id.slide_button:
			int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());
			SlideMenuActivity.prepare(MainActivity.this, R.id.inner_content, width);
			startActivity(new Intent(MainActivity.this,
					MenuActivity.class));
			overridePendingTransition(0, 0);			
			break;		
		}		
	}
	 public void getScrollMaxAmount(){
	    	int actualWidth = (horizontalOuterLayout.getMeasuredWidth()-512);
	    	scrollMax   = actualWidth;
	    }
	    
	    public void startAutoScrolling(){
			if (scrollTimer == null) {
				scrollTimer					=	new Timer();
				final Runnable Timer_Tick 	= 	new Runnable() {
				    public void run() {
				    	moveScrollView();
				    }
				};
				
				if(scrollerSchedule != null){
					scrollerSchedule.cancel();
					scrollerSchedule = null;
				}
				scrollerSchedule = new TimerTask(){
					@Override
					public void run(){
						runOnUiThread(Timer_Tick);
					}
				};
				
				scrollTimer.schedule(scrollerSchedule, 30, 30);
			}
		}
	    
	    public void moveScrollView(){
	    	scrollPos							= 	(int) (horizontalScrollview.getScrollX() + 1.0);
			if(scrollPos >= scrollMax){
				scrollPos						=	0;
			}
			horizontalScrollview.scrollTo(scrollPos, 0);
					
		}	    
	   
	    public void addImagesToView(){
			for (int i=0;i<imageNameArray.length;i++){
				final Button imageButton =	new Button(this);
				int imageResourceId		 =	getResources().getIdentifier(imageNameArray[i], "drawable",getPackageName());
			    Drawable image 			 =	this.getResources().getDrawable(imageResourceId);
			    imageButton.setBackgroundDrawable(image);
			    imageButton.setTag(i);
			    imageButton.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View arg0) {
						if(isFaceDown){
							if(clickTimer!= null){
								clickTimer.cancel();
								clickTimer			=	null;
							}
							clickedButton			=	(Button)arg0;
							stopAutoScrolling();
							clickedButton.startAnimation(scaleFaceUpAnimation());
							clickedButton.setSelected(true);
							clickTimer				=	new Timer();
							
							if(clickSchedule != null) {
								clickSchedule.cancel();
								clickSchedule = null;
							}
							
							clickSchedule = new TimerTask(){
					        	public void run() { 
					        		//startAutoScrolling();   
					            }
					        };
							
							clickTimer.schedule( clickSchedule, 1500);
						}
					}
				});
				
				LinearLayout.LayoutParams params 	=	new LinearLayout.LayoutParams(256,256);
				params.setMargins(0, 25, 0, 25);
				imageButton.setLayoutParams(params);
				horizontalOuterLayout.addView(imageButton);
			}
		}
	    
	    public Animation scaleFaceUpAnimation(){
			Animation scaleFace = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
			scaleFace.setDuration(500);
			scaleFace.setFillAfter(true);
			scaleFace.setInterpolator(new AccelerateInterpolator());
			Animation.AnimationListener	scaleFaceAnimationListener = new Animation.AnimationListener() {			
				@Override
				public void onAnimationStart(Animation arg0) {					
					isFaceDown = false;
				}			
				@Override
				public void onAnimationRepeat(Animation arg0) {}			
				@Override
				public void onAnimationEnd(Animation arg0) {
					if(faceTimer != null){
						faceTimer.cancel();
						faceTimer = null;
					}
					
					faceTimer = new Timer();
					if(faceAnimationSchedule != null){
						faceAnimationSchedule.cancel();
						faceAnimationSchedule = null;
					}
					faceAnimationSchedule = new TimerTask() {					
						@Override
						public void run() {
							faceScaleHandler.sendEmptyMessage(0); 										
						}
					};
					
					faceTimer.schedule(faceAnimationSchedule, 750);				
				}
			}; 
			scaleFace.setAnimationListener(scaleFaceAnimationListener);
			return scaleFace;
		}
	    
	    private Handler faceScaleHandler = new Handler() {
	        @Override
	        public void handleMessage(Message msg) {
	        	if(clickedButton.isSelected() == true)
	        		clickedButton.startAnimation(scaleFaceDownAnimation(500));		
	        }
		};
		
		public Animation scaleFaceDownAnimation(int duration){
			Animation scaleFace = new ScaleAnimation(1.2f, 1.0f, 1.2f, 1.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
			scaleFace.setDuration(duration);
			scaleFace.setFillAfter(true);
			scaleFace.setInterpolator(new AccelerateInterpolator());
			Animation.AnimationListener	scaleFaceAnimationListener = new Animation.AnimationListener() {			
				@Override
				public void onAnimationStart(Animation arg0) {}			
				@Override
				public void onAnimationRepeat(Animation arg0) {}			
				@Override
				public void onAnimationEnd(Animation arg0) {				
					isFaceDown = true;				
				}
			}; 
			scaleFace.setAnimationListener(scaleFaceAnimationListener);
			return scaleFace;
		}
	    
	    public void stopAutoScrolling(){
			if (scrollTimer != null) {
				scrollTimer.cancel();
				scrollTimer	=	null;
			}
		}
	    
	    public void onBackPressed(){
			super.onBackPressed();
			finish();
		}
		
		public void onPause() {
			super.onPause();
			finish();
		}
		
		public void onDestroy(){
			clearTimerTaks(clickSchedule);
			clearTimerTaks(scrollerSchedule);
			clearTimerTaks(faceAnimationSchedule);		
			clearTimers(scrollTimer);
			clearTimers(clickTimer);
			clearTimers(faceTimer);
			
			clickSchedule         = null;
			scrollerSchedule      = null;
			faceAnimationSchedule = null;
			scrollTimer           = null;
			clickTimer            = null;
			faceTimer             = null;
			super.onDestroy();
		}
		
		private void clearTimers(Timer timer){
		    if(timer != null) {
			    timer.cancel();
		        timer = null;
		    }
		}
		
		private void clearTimerTaks(TimerTask timerTask){
			if(timerTask != null) {
				timerTask.cancel();
				timerTask = null;
			}
		}
}
